<template>
	<view class="content columnbox wdh-100 jus">
		<!-- <swiper class="swiper-box" autoplay indicator-dots>
			<swiper-item v-for="(item,index) in posterList" :key='index'>
				<image :src="item.image" mode="aspectFill"></image>
			</swiper-item>
		</swiper> -->
		<view class="search-box wdh-100 rowbox spb">
			<view class="search-input rowbox">
				<u-icon name="search" size="32rpx"></u-icon>
				<input v-model="searchvalue" placeholder="苹果" />
			</view>
			<view class="def-col search-btn" @click="searchInfo">搜索</view>
		</view>
		<view class="rowbox wdh-100 jus rank-box">
			<view @click="setRank(1)" class="one-rank rowbox" :class="{'sel-rank':currank==1}">综合排序</view>
			<view @click="setRank(currank==2?3:2)" class="one-rank rowbox" :class="{'sel-rank':currank>1}">价格
				<view class="columnbox rank-icon">
					<view v-if="currank==2">
						<u-icon color="#57bd6a" name="arrow-up-fill" size="20rpx"></u-icon>
					</view>
					<view v-else>
						<u-icon color="#bdbfc1" name="arrow-up-fill" size="20rpx"></u-icon>
					</view>
					<view v-if="currank==3">
						<u-icon color="#57bd6a" name="arrow-down-fill" size="20rpx"></u-icon>
					</view>
					<view v-else>
						<u-icon color="#bdbfc1" name="arrow-down-fill" size="20rpx"></u-icon>
					</view>
				</view>
			</view>
		</view>
		<view class="wdh-100 tab-box" :class="{'fix-topbox':topflag}">
			<u-tabs lineColor='#57bd6a' @change="changeType" :list="typeList" :activeStyle="{
			    color: '#303133',
				fontSize:'28rpx'
			}" :inactiveStyle="{
			    color: '#afb3ba',
				fontSize:'28rpx', 
			}" itemStyle="padding-left: 15px; padding-right: 15px; height: 34px;"></u-tabs>
		</view>
		<view v-for="(item,index) in articleList" :key='index' class="columnbox one-box">
			<view class="rowbox wdh-100 spb als">
				<image :src="item.pic" mode="aspectFill" class="info-img"></image>
				<view class="columnbox info-detail">
					<text class="info-title">{{item.title}}</text>
					<view class="info-price">价格：¥{{item.price}}</view>
				</view>
			</view>
			<view class="rowbox wdh-100 spb manage-box">
				<view>{{item.addtime}}</view>
				<view>来源：{{item.laiyuan}}</view>
				<view class="def-bgcol rowbox one-btn" @click="copyLink(item.url)">复制链接</view>
			</view>
		</view>
		<kefubox ref="kefu"></kefubox>
		<noticeWeak ref="noticeWeak"></noticeWeak>
		<!-- vip弹窗 -->
		<vipBox ref="vipbuy"></vipBox>
	</view>
</template>
<script>
	import vipBox from '@/components/vipbuy.vue'
	import noticeWeak from '@/components/weaknotice.vue'
	import kefubox from '@/components/kefu.vue'
	import gettime from '@/utils/time.js'
	export default {
		components: {
			kefubox,
			noticeWeak,
			vipBox
		},
		data() {
			return {
				currank: 1,
				posterList: [],
				typeList: [],
				pageNo: 1,
				curtype: 0,
				articleList: [],
				total: 0,
				topflag: false,
				searchvalue: '',
				searchflag: false,
				shareinfo: null
			}
		},
		onLoad(options) {
			if (options.pid) {
				uni.setStorageSync('pid', options.pid)
			}
			// this.getBanner()
			this.getTypeList()
			this.getShareInfo()
			this.$nextTick(() => {
				this.$refs.noticeWeak.getUserInfo()
			})
		},
		onShareAppMessage() {
			return {
				title: this.shareinfo.title,
				imageUrl: this.shareinfo.image,
				path: '/pages/consumables/consumables?pid=' + uni.getStorageSync('uid')
			}
		},
		onShareTimeline() {
			return {
				title: this.shareinfo.title,
				imageUrl: this.shareinfo.image,
				path: '/pages/consumables/consumables?pid=' + uni.getStorageSync('uid')
			}
		},
		onShow() {
			uni.hideLoading()
		},
		onReachBottom() {
			if (this.articleList.length < this.total) {
				this.pageNo++
				this.getArticle()
			}
		},
		onPullDownRefresh() {
			this.searchflag = false
			this.searchvalue = ''
			this.pageNo = 1
			this.articleList = []
			this.getArticle()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 500)
		},
		onPageScroll(e) {
			if (!this.topflag) {
				if (e.scrollTop > 200) {
					this.topflag = true
				}
			} else {
				if (e.scrollTop < 200) {
					this.topflag = false
				}
			}
		},
		methods: {
			setRank(rank) {
				this.currank = rank
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 100
				})
				this.pageNo = 1
				this.articleList = []
				this.getArticle()
			},
			copyLink(url) {
				// iOS
				if (getApp().globalData.platform == 'ios') {
					this.$req.post('/shanghu/userInfo', {
						uid: uni.getStorageSync('uid')
					}).then(res => {
						uni.removeStorageSync('userInfo')
						uni.setStorageSync('userInfo', res.data.data)
						if (res.data.data.vip == 1) {
							this.$refs.kefu.show = true
						} else {
							if (res.data.data.phone == '') {
								uni.navigateTo({
									url: '/pages/bindPhone/bindPhone'
								})
							} else {
								if (res.data.data.status == 2) {
									this.$refs.kefu.show = true
									this.$refs.kefu.type = 2
								} else {
									this.$req.post('/shanghu/dianji', {
										uid: uni.getStorageSync('uid'),
										state: 2
									})
									uni.setClipboardData({
										data: url,
										success() {}
									})
								}
							}
						}
					})
				} else {
					// 安卓
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					this.$req.post('/shanghu/userInfo', {
						uid: uni.getStorageSync('uid')
					}).then(res => {
						uni.removeStorageSync('userInfo')
						uni.setStorageSync('userInfo', res.data.data)
						uni.hideLoading()
						if (res.data.data.vip == 1) {
							this.$refs.vipbuy.show = true
						} else {
							if (res.data.data.status == 2) {
								this.$refs.kefu.show = true
								this.$refs.kefu.type = 2
							} else {
								this.$req.post('/shanghu/dianji', {
									uid: uni.getStorageSync('uid'),
									state: 2
								})
								// 已开通会员并绑定手机号
								uni.setClipboardData({
									data: url,
									success() {}
								})
							}
						}
					})
				}
			},
			searchInfo() {
				if (this.searchvalue == '') {
					uni.showToast({
						title: '请输入搜索内容',
						icon: 'none'
					})
				} else {
					uni.pageScrollTo({
						scrollTop: 0,
						duration: 100
					})
					this.searchflag = true
					this.pageNo = 1
					this.articleList = []
					this.getArticle()
				}
			},
			getShareInfo() {
				this.$req.post('/shanghu/zhuanfahc').then(res => {
					this.shareinfo = res.data.data
				})
			},



			changeType(e) {
				this.curtype = e.index
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 100
				})
				this.pageNo = 1
				this.articleList = []
				this.getArticle()
			},
			getTypeList() {
				this.$req.post('/shanghu/typeconlist').then(res => {
					this.typeList = res.data.data
					this.getArticle()
				})
			},
			getBanner() {
				this.$req.post('/shanghu/bannerlist', {
					status: 3
				}).then(res => {
					this.posterList = res.data.data
				})
			},
			getArticle() {
				let data = {
					pageNo: this.pageNo,
					pageSize: 10,
					name: this.searchflag ? this.searchvalue : '',
					typeid: this.typeList[this.curtype].id,
					sort: this.currank - 1
				}
				this.$req.post('/shanghu/consumableslist', data).then(res => {
					res.data.data.data.forEach(resp => {
						resp.pic = resp.image.split(',')[0]
						resp.addtime = gettime.toLocalDate(resp.addtime)
					})
					this.articleList = this.articleList.concat(res.data.data.data)
					this.total = res.data.data.totalCount
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background: #f6f6f6;
		min-height: 100vh;
	}

	.swiper-box {
		margin-top: 20rpx;
		width: 710rpx;
		height: 370rpx;
		border-radius: 20rpx;
		overflow: hidden;

		image {
			width: 100%;
			height: 370rpx;
		}
	}

	.tab-box {
		background: #fff;
	}

	.one-box {
		width: 100%;
		padding: 20rpx 20rpx;
		background: #fff;
		margin: 10rpx 0;
	}

	.article-content {
		font-size: 28rpx;
		line-height: 40rpx;
		color: #2c2c2c;
		margin-bottom: 20rpx;
	}

	.img-one {
		height: 400rpx;
		width: 100%;
		border-radius: 10rpx;
	}


	.manage-box {
		margin-bottom: 20rpx;
		font-size: 24rpx;
		color: #9fa1a3;
		margin-top: 20rpx;
	}

	.fix-topbox {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 10;
	}

	.search-box {
		background: #fff;
		padding: 20rpx 20rpx;

		.search-input {
			background: #f6f6f6;
			padding: 0 20rpx;
			border-radius: 20rpx;
			width: 600rpx;

			input {
				margin-left: 10rpx;
				width: calc(100% - 40rpx);
				font-size: 28rpx;
				height: 70rpx;
				line-height: 70rpx;
			}
		}
	}

	.search-btn {
		font-size: 28rpx;
	}

	.info-img {
		width: 150rpx;
		height: 150rpx;
		border-radius: 10rpx;
		margin-right: 20rpx;
	}

	.info-detail {
		width: calc(100% - 170rpx);
	}

	.info-title {
		width: 100%;
		font-size: 26rpx;
	}

	.info-price {
		width: 100%;
		text-align: right;
		color: #fc0007;
		margin-top: 20rpx;
	}

	.one-btn {
		color: #fff;
		padding: 10rpx 20rpx;
		border-radius: 100rpx;
	}

	.rank-box {
		background: #fff;
		padding: 20rpx 20rpx;
		border-bottom: #edeff1 solid 1px;

		.one-rank {
			font-size: 28rpx;
			color: #bdbfc1;
			margin-right: 70rpx;
			line-height: 50rpx;
		}

		.sel-rank {
			color: #000;
		}

		.rank-icon {
			margin-left: 10rpx;
			transform: scale(.9);
		}
	}
</style>